﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

namespace FontTool
{
    public partial class FontToolMain
    {
        #region variables initialization
        string uriDummySets = @"XML\DummySet.xml";
        Dictionary<string, dummySet> DummySets = new Dictionary<string, dummySet>();
        string currentDummySet = "Default";
        bool useDummy = true;
        int dummyNumberOFImages = 0;
        #endregion

        #region Dummy dictionary\comboBox\CheckBox init
        private void comboBoxDummySetsInit()
        {
            comboBoxDummySets.Items.Clear();
            foreach (var c in DummySets)
                comboBoxDummySets.Items.Add(c.Key);
            comboBoxDummySets.SelectedIndex = comboBoxDummySets.Items.IndexOf("Default");
        }

        private void checkBoxDummy_CheckedChanged(object sender, EventArgs e)
        {
            switch (checkBoxDummy.CheckState)
            {
                case CheckState.Checked:
                    if (newFont != null)
                    {
                        useDummy = true;
                        updateDataGridView();
                    }
                    break;
                case CheckState.Unchecked:
                    if (newFont != null)
                    {
                        useDummy = false;
                        updateDataGridView();
                    }
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region dummy dataGridView initialization & load images
        private void dummyDataGridViewInit()
        {
            dataGridViewGlyphs.Rows.Clear();
            dataGridViewGlyphs.Columns.Clear();

            if (currentFilter != "NONE")
                dummyNumberOFImages = Filters[currentFilter].Count;
            else
                dummyNumberOFImages = DummySets[currentDummySet].Count;

            int columnsPerWidth = (dataGridViewGlyphs.Width - 10) / imgWidth;//(imgWidth + 20);
            int columnWidth = dataGridViewGlyphs.Size.Width / columnsPerWidth;
            int numberOfRows = 0;

            numberOfRows = dummyNumberOFImages / columnsPerWidth;

            // Do we have a an overfill for a row
            if (dummyNumberOFImages % columnsPerWidth > 0)
            {
                numberOfRows += 1;
            }

            dummyNumberOFImages = numberOfRows * columnsPerWidth;

            // Dynamically create the columns
            for (int index = 0; index < columnsPerWidth; index++)
            {
                customDataGridViewImageColumn dataGridViewColumn = new customDataGridViewImageColumn();
                dataGridViewGlyphs.Columns.Add(dataGridViewColumn);
                dataGridViewGlyphs.Columns[index].Width = columnWidth;
            }

            // Create the rows
            for (int index = 0; index < numberOfRows; index++)
            {
                dataGridViewGlyphs.Rows.Add();
                dataGridViewGlyphs.Rows[index].Height = columnWidth;
            }
        }

        private void dummyLoadImagesToGridView()
        {
            int columnIndex = 0;
            int rowIndex = 0;
            for (int index = 0; index < DummySets[currentDummySet].Count; index++)
            {
                if (currentFilter == "NONE")
                {
                    Image image = Helper.ResizeImage(Helper.drawImageFromHex(DummySets[currentDummySet].Characters[index]), imgWidth, imgWidth, false);

                    dataGridViewGlyphs.Rows[rowIndex].Cells[columnIndex].Value = image;
                    //need test
                    (dataGridViewGlyphs.Rows[rowIndex].Cells[columnIndex] as customDataGridViewCell).cSource = customDataGridViewCell.cellSource.Dummy;
                    dataGridViewGlyphs.Rows[rowIndex].Cells[columnIndex].ToolTipText = Helper.stringToHex(DummySets[currentDummySet].Characters[index].ToString())[0];

                    if (columnIndex == dataGridViewGlyphs.Columns.Count - 1)
                    {
                        rowIndex += 1;
                        columnIndex = 0;
                    }
                    else
                    {
                        columnIndex++;
                    }
                }

                if (currentFilter != "NONE")
                {
                    if (Filters[currentFilter].Hexs.Contains(Helper.charToHex(DummySets[currentDummySet].Characters[index])))
                    {
                        Image image = Helper.ResizeImage(Helper.drawImageFromHex(DummySets[currentDummySet].Characters[index]), imgWidth, imgWidth, false);

                        dataGridViewGlyphs.Rows[rowIndex].Cells[columnIndex].Value = image;
                        //need test
                        (dataGridViewGlyphs.Rows[rowIndex].Cells[columnIndex] as customDataGridViewCell).cSource = customDataGridViewCell.cellSource.Dummy;
                        dataGridViewGlyphs.Rows[rowIndex].Cells[columnIndex].ToolTipText = Helper.stringToHex(DummySets[currentDummySet].Characters[index].ToString())[0];

                        if (columnIndex == dataGridViewGlyphs.Columns.Count - 1)
                        {
                            rowIndex += 1;
                            columnIndex = 0;
                        }
                        else
                        {
                            columnIndex++;
                        }
                    }
                }
            }

            //takes care of empty cells
            foreach (DataGridViewRow row in dataGridViewGlyphs.Rows)
                foreach (customDataGridViewCell cell in row.Cells)
                    if (cell.Value == null)
                    {
                        DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
                        dataGridViewCellStyle.NullValue = null;
                        dataGridViewCellStyle.Tag = "BLANK";
                        cell.Style = dataGridViewCellStyle;
                        cell.cSource = customDataGridViewCell.cellSource.Dummy;
                    }
        }
        #endregion
    }
}
